package com.lzy.elasticsearch.service.impl;

import com.lzy.elasticsearch.service.ElasticsearchService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.client.indices.PutMappingRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Arrays;

/**
 * TODO 1.可以再封装，request对象在实现类里面创建，外面调用只需要传入需要的参数即可
 *      2.日志采用aop方式注入把日志对象也注入到aop防止多个日志对象
 * @author L
 */
@Slf4j
@Component
public class ElasticsearchServiceImpl implements ElasticsearchService {

    @Autowired
    private RestHighLevelClient client;

    @Override
    public boolean existsIndex(GetIndexRequest request) {
        return false;
    }

    @Override
    public void createIndex(CreateIndexRequest request) {

    }

    @Override
    public void deleteIndex(DeleteRequest request) {

    }

    @Override
    public GetIndexResponse getIndex(GetIndexRequest request) {
        try {
            log.debug("es 查询index");
            log.info("index-->{}", Arrays.toString(request.indices()));
            GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
            log.info("elasticsearch 查询索引成功-->{}", Arrays.toString(request.indices()));
            return response;
        } catch (IOException e) {
            log.error("es 获取index失败-->", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateIndexAliases(IndicesAliasesRequest request) {

    }

    @Override
    public void putIndexMapping(PutMappingRequest request) {

    }

    @Override
    public IndexResponse addDoc(IndexRequest request) {
        return null;
    }

    @Override
    public DeleteResponse deleteDoc(DeleteRequest request) {
        return null;
    }

    @Override
    public SearchResponse searchDoc(SearchRequest request) {
        return null;
    }

    @Override
    public SearchResponse scrollDoc(SearchScrollRequest request) {
        return null;
    }

    @Override
    public GetResponse getDoc(GetRequest request) {
        return null;
    }

    @Override
    public UpdateResponse updateDoc(UpdateRequest request) {
        return null;
    }

    @Override
    public BulkResponse bulkOperation(BulkRequest request) {
        return null;
    }
}
